package api

import (
	"lianzi_gos/internal/mods/mpweixin/biz"
	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/util"

	"github.com/gin-gonic/gin"
)

// 我要买
type Procure struct {
	ProcureBIZ *biz.Procure
}

// @Tags ProcureAPI
// @Security ApiKeyAuth
// @Summary Query procure list
// @Param name query string false "购买名称"
// @Success 200 {object} util.ResponseResult{data=[]schema.Procure}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/procures [get]
func (a *Procure) QueryAdmin(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.ProcureQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.ProcureBIZ.QueryAdmin(ctx, params)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResPage(c, result.Data, result.PageResult)
}

// @Tags ProcureAPI
// @Security ApiKeyAuth
// @Summary Get procure record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult{data=schema.Procure}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/procures/{id} [get]
func (a *Procure) Get(c *gin.Context) {
	ctx := c.Request.Context()
	item, err := a.ProcureBIZ.Get(ctx, c.Param("id"))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, item)
}

// @Tags ProcureAPI
// @Security ApiKeyAuth
// @Summary Create procure record
// @Param body body schema.ProcureForm true "Request body"
// @Success 200 {object} util.ResponseResult{data=schema.Procure}
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/procures [post]
func (a *Procure) Create(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.ProcureForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.ProcureBIZ.Create(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, result)
}

// @Tags ProcureAPI
// @Security ApiKeyAuth
// @Summary Update procure record by ID
// @Param id path string true "unique id"
// @Param body body schema.ProcureForm true "Request body"
// @Success 200 {object} util.ResponseResult
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/procures/{id} [put]
func (a *Procure) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.ProcureForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	err := a.ProcureBIZ.Update(ctx, c.Param("id"), item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags ProcureAPI
// @Security ApiKeyAuth
// @Summary Delete procure record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/procures/{id} [delete]
func (a *Procure) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	err := a.ProcureBIZ.Delete(ctx, c.Param("id"))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
